// pages/comment_order/comment_order.js
const api = require('../../utils/util.js');
const service = require('../../utils/service.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    submitInfo: '',
    goodImage: '',
    imgList: [],
    score: '',
    anonymous: 0,
    des_score: '',
    log_score: '',
    ser_score: '',
    submit_flag: false,
  },

  //--点击 上方 好评中评差评
  click_score: function(e) {
    var item = e.currentTarget.dataset;
    if (this.data.score == item.score) {
      this.setData({
        score: ''
      })
    } else {
      this.setData({
        score: item.score
      })
    }
  },
  //--添加图片
  addImg: function() {
    let _this = this;
    if(this.data.imgList.length >= 8){
    	api.toast("最多可以上传8张！");
    	return;
    }
    wx.showActionSheet({
      itemList: ['拍照', '从相册中选择'],
      success: function(res) {
        if (res.tapIndex == 0) {
          api.upLoadImg(8, 'camera', function(res) {
            console.log(res)
            let imgList = _this.data.imgList.push(res)
            _this.setData({
              imgList: imgList
            })
          })
        } else if (res.tapIndex == 1) {
          api.upLoadImg(8, 'album', function(res) {
            let imgList = _this.data.imgList.slice();
            for (let i = 0; i < res.length;i++){
              imgList.push(res[i])
            }

            _this.setData({
              imgList: imgList
            })
          })
        }
      }
    })
  },

  delImg: function(e){
    var data = e.currentTarget.dataset;
    var imgList = this.data.imgList;
    imgList.splice(data.inds,1)
    this.setData({
      imgList: imgList
    })
  },

  //--是否匿名
  change: function(e) {
    var tag = 1;
    if (e.detail.value == '') {
      tag = 0;
    }
    this.setData({
      anonymous: tag
    })
  },

  //--点击下方的描述相符星星
  descriptScore: function(e) {
    var item = e.currentTarget.dataset;
    this.setData({
      des_score: item.index
    })
  },
  //--点击下方的物流服务星星
  logisticScore: function(e) {
    var item = e.currentTarget.dataset;
    this.setData({
      log_score: item.index
    })
  },
  //--点击下方的服务态度星星
  serviceScore: function(e) {
    var item = e.currentTarget.dataset;
    this.setData({
      ser_score: item.index
    })
  },

  formSubmit: function(e) {
    console.log(e.detail.value);
    let data = e.detail.value;
    let submitInfo = this.data.submitInfo;

    if (this.data.submit_flag){
      api.toast('已经提交，请耐心等待...')
      return;
    }
    this.setData({
      submit_flag: true
    })

    if (this.data.score == '') {
      api.toast('请选择评价');
      return;
    } else if (this.data.des_score == '') {
      api.toast('请为描述相符打分');
      return;
    } else if (this.data.log_score == '') {
      api.toast('请为物流服务打分');
      return;
    } else if (this.data.ser_score == '') {
      api.toast('请为服务态度打分');
      return;
    }

    submitInfo.score = this.data.score;
    submitInfo.content = data.comment;
    let imgList = this.data.imgList;
    submitInfo.images = imgList.join(',');
    submitInfo.is_anony = this.data.anonymous;
    submitInfo.star = this.data.des_score + '|' + this.data.log_score + '|' + this.data.ser_score;

    api.ajax(service.url('submitGoodsComment'), {
      score: submitInfo.score,
      content: submitInfo.content,
      images: submitInfo.images,
      is_anony: submitInfo.is_anony,
      star: submitInfo.star,
      goods_id: submitInfo.goods_id,
      order_id: submitInfo.order_id,
    }, function(res) {
      console.log(res)
      wx.showModal({
        title: '信息提示',
        showCancel: false,
        content: '提交评价成功！',
        success: function(res) {
          if (res.confirm) {
            wx.navigateBack({
            	delta: 2
            });
          }
        }
      })
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var submitInfo = {
      goods_id: options.good_id,
      order_id: options.order_id,
    }

    this.setData({
      submitInfo: submitInfo,
      goodImage: options.goods_image
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})